//
//  SNVideoViewController.m
//  DayDayHappy
//
//  Created by yutao on 14-2-19.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "SNVideoViewController.h"
#import "SNImageModel.h"

@interface SNVideoViewController ()
{
    NSMutableArray *videoPlayArray;
}
@end

@implementation SNVideoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            [self.dbHelper createTableWithModelClass:[SNThemeModel class]];
//        });
        [self.dbHelper createTableWithModelClass:[SNVideoModel class]];
        self.requestUrl = [NSString stringWithFormat:@"%@%@?type=%@",kBaseUrl,kGetContent,kVideoModle];
        videoPlayArray = [[NSMutableArray alloc] initWithCapacity:1];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (NSString *)currentType
{
    return kVideoModle;
}

- (SNBaseModel *)currModelWithObj:(id)obj
{
    return [[SNVideoModel alloc] initWithDictionary:obj error:nil];
}
-(Class)currModel
{
    return [SNVideoModel class];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentif = @"cell";
    SNVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentif];
    if (cell == nil) {
        cell = [[SNVideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentif];
    }
    cell.delegate = self;
    [cell setUpImageCellWithObject:self.contentDataArray[indexPath.row] atIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
        SNVideoCell *videoCell = (SNVideoCell *)cell;
        [videoCell stopPlayerController];
}

- (void)videoCellDidClick:(SNVideoCell *)cell
{
    if (videoPlayArray.count>0) {
        SNVideoCell *videoCell = [videoPlayArray objectAtIndex:0];
        [videoCell stopPlayerController];
        [videoPlayArray removeAllObjects];
        [videoPlayArray addObject:cell];
    }else{
        [videoPlayArray addObject:cell];
    }
}

- (void)videoDidStop
{
    [videoPlayArray removeAllObjects];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
